package com.hzdracom.zhangnn.animationdemo;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView   ivTest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivTest  = (ImageView) findViewById(R.id.test_image);
        ivTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startAnim2();
            }
        });

    }

    private void startAnim() {
        Animation as = AnimationUtils.loadAnimation(this,R.anim.scale);
        as.setRepeatCount(6);
        as.setRepeatMode(Animation.RESTART);
        as.setInterpolator(new LinearInterpolator());
        ivTest.startAnimation(as);
    }

    private void startAnim2() {
        AnimationSet  as = new AnimationSet(true);
        AlphaAnimation  alpha = new AlphaAnimation(0.2f,1f);
//        ObjectAnimator  alpha2 = ObjectAnimator.ofFloat(ivTest,"Alpha",0.2f,1.0f,0.2f);
        ScaleAnimation  scale = new ScaleAnimation(1.0f,4.0f,1.0f,4.0f,2,1f,2,1f);
        /**
         * 后四位的作用，5 7 表示XY轴缩放的参考对象，1 为自己，2为父类默，其他值默认为1；
         * 6,8位 表示缩放点坐标，取值0-1 （0.5，0.5） 为中心点
         */
        /**
         * 对比发现，AnimationSet 没有animator 灵活，
         */
        scale.setRepeatMode(Animation.REVERSE);
        scale.setRepeatCount(0);
        scale.setDuration(4000);
        as.setInterpolator(new LinearInterpolator());
        as.addAnimation(alpha);
        as.addAnimation(scale);
        as.setDuration(4000);
        ivTest.startAnimation(as);
    }

    private void startAnim3(){
        AnimatorSet   as = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(ivTest,"scaleX",1.0f,4.0f,2.0f,3.0f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(ivTest,"scaleY",1.0f,4.0f,2.0f,3.0f);

        scaleX.setDuration(4000);
        scaleX.setRepeatCount(3);
        scaleX.setRepeatMode(Animation.RESTART);

        scaleY.setDuration(4000);//此处的时间会被AnimatorSet 的时间覆盖掉
        scaleY.setRepeatCount(3);
        scaleY.setRepeatMode(Animation.RESTART);

        as.playTogether(scaleX,scaleY);
//        as.setTarget(ivTest);
        as.setDuration(8000);//此处设置了时间之后，前面单独设置的时间无效
        as.setInterpolator(new AccelerateInterpolator());
        as.start();
    }
}
